package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.SystemAlerts;
import com.example.mall.dto.SystemAlertsDto;
import com.example.mall.mapper.SystemAlertsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SystemAlertsQuery;
import com.example.mall.service.ISystemAlertsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 异常报警记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class SystemAlertsServiceImpl extends ServiceImpl<SystemAlertsMapper, SystemAlerts> implements ISystemAlertsService {

    @Autowired
    private SystemAlertsMapper systemAlertsMapper;

    @Override
    public PageDTO<SystemAlertsDto> queryPage(SystemAlertsQuery systemAlertsQuery) {
        if (systemAlertsQuery == null) {
            systemAlertsQuery = new SystemAlertsQuery();
        }
        Page<SystemAlerts> page = systemAlertsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SystemAlerts> cartPage = lambdaQuery()
                .like(systemAlertsQuery.getAlertId()!= null, SystemAlerts::getAlertId, systemAlertsQuery.getAlertId())
                .like(systemAlertsQuery.getAlertType()!= null, SystemAlerts::getAlertType, systemAlertsQuery.getAlertType())
                .like(systemAlertsQuery.getDescription()!= null, SystemAlerts::getDescription, systemAlertsQuery.getDescription())
                .like(systemAlertsQuery.getSeverity()!= null, SystemAlerts::getSeverity, systemAlertsQuery.getSeverity())
                .like(systemAlertsQuery.getHandledStatus()!= null, SystemAlerts::getHandledStatus, systemAlertsQuery.getHandledStatus())
                .like(systemAlertsQuery.getHandledBy()!= null, SystemAlerts::getHandledBy, systemAlertsQuery.getHandledBy())
                .apply(systemAlertsQuery.getAlertTime() != null,
                        "DATE_FORMAT(alert_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        systemAlertsQuery.getAlertTime())
                .apply(systemAlertsQuery.getHandledTime() != null,
                        "DATE_FORMAT(handled_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        systemAlertsQuery.getHandledTime())
                .orderByDesc(SystemAlerts::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, SystemAlertsDto.class);
    }

    @Override
    public SystemAlerts querySystemAlertsById(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        return systemAlertsMapper.selectById(id);
    }

    @Override
    public List<SystemAlerts> querySystemAlertsList() {
        return this.list();
    }

    @Override
    public void insert(SystemAlertsDto systemAlertsDto) {
        SystemAlerts systemAlerts = new SystemAlerts();
        CopyUtils.copyProperties(systemAlertsDto, systemAlerts);
        systemAlerts.setAlertId(CodeGenerateUtils.generateUniqueId());
        systemAlerts.setCreateDate(new Date());
        systemAlerts.setUpdateDate(new Date());
        systemAlerts.setHandledStatus(BaseConstants.NOT_USED);
        systemAlertsMapper.insert(systemAlerts);
    }

    @Override
    public void update(SystemAlertsDto systemAlertsDto) {
        AssertUtil.isNotNull(systemAlertsDto.getAlertId(),"唯一标识不能为空");
        SystemAlerts systemAlerts = systemAlertsMapper.selectById(systemAlertsDto.getAlertId());
        AssertUtil.isNotNull(systemAlerts,"唯一标识不存在");
        CopyUtils.copyProperties(systemAlertsDto, systemAlerts);
        systemAlerts.setUpdateDate(new Date());
        systemAlertsMapper.updateById(systemAlerts);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        SystemAlerts systemAlerts = systemAlertsMapper.selectById(id);
        AssertUtil.isNotNull(systemAlerts,"唯一标识不存在");
        systemAlerts.setHandledStatus(BaseConstants.USED_ALREADY);
        systemAlerts.setHandledTime(new Date());
        systemAlertsMapper.updateById(systemAlerts);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids,"唯一标识不能为空");
        List<SystemAlerts> systemAlerts = systemAlertsMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(systemAlerts,"唯一标识不存在");
        systemAlerts.forEach(item -> {
            item.setHandledStatus(BaseConstants.USED_ALREADY);
            item.setHandledTime(new Date());
            systemAlertsMapper.updateById(item);
        });
    }
}
